<template>
<div>
  <van-nav-bar
    title="收货地址"
    left-arrow
    @click-left="return1"
    :fixed="true" :placeholder="true"
  />
  <van-address-list
    v-model="chosenAddressId"
    :list="list"
    default-tag-text="默认"
    @select="clickOne"
    @add="onAdd"
    @edit="onEdit"
  />
</div>
</template>
<script>
import Vue from 'vue';
import {AddressList, Toast} from 'vant';
import { NavBar } from 'vant';
import request from "../../utils/request";

Vue.use(NavBar);
Vue.use(AddressList);
export default {
  data() {
    return {
      orderId:this.$route.query.orderId,
      i:this.$route.query.i,//detailId
      itemId:this.$route.query.itemId,
      chosenAddressId: '',
      list: [],
    };
  },
  created() {
    this.getData()
  },
  methods: {
    getData(){
      request.get('/address/getData').then(resp=>{
        if (resp.code===0){
          this.list=resp.data
          for (let i = 0; i < resp.data.length; i++) {
            if (resp.data[i].addressId==this.$route.query.addressId){
              this.chosenAddressId=""+i
            }
            this.list[i].tel=this.list[i].phoneNumber
            this.list[i].addressDetail=this.list[i].streetAddress
            this.list[i].id=this.list[i].addressId
            this.list[i].name=this.list[i].recipientName
            this.list[i].isDefault=this.list[i].district=='1'?true:false
            this.list[i].address=this.list[i].province+this.list[i].city+this.list[i].country+this.list[i].streetAddress

          }
        }
      })
    },
    clickOne(item,index){
     request.post('/order/updateAddress',{addressId:item.addressId,orderId:this.$route.query.orderId}).then(resp=>{
       this.$router.push({path:'/appOrder',query:{orderId:this.$route.query.orderId,i:this.i,itemId:this.itemId}})
      Toast(resp.errorMessage)
     })

    },
    onAdd() {
      if (this.itemId){
        this.$router.push({path:'/AddressEdit',query:{i:this.i,itemId:this.itemId,orderId:this.orderId}})
      }else {
      this.$router.push({path:'/AddressEdit',query:{orderId:this.orderId}})
      }
    },
    onEdit(item, index) {
      if (this.itemId){
        this.$router.push({path: '/AddressEdit', query: {name: this.list[index].name,
            tel:this.list[index].tel,id:this.list[index].id,province:this.list[index].province,city:this.list[index].city,
            county:this.list[index].county, addressDetail:this.list[index].addressDetail,isDefault:this.list[index].isDefault,
            areaCode:this.list[index].areaCode,i:this.i,itemId:this.itemId,orderId:this.orderId
          }})
      }else {
        this.$router.push({path: '/AddressEdit', query: {name: this.list[index].name,
            tel:this.list[index].tel,id:this.list[index].id,province:this.list[index].province,city:this.list[index].city,
            county:this.list[index].county, addressDetail:this.list[index].addressDetail,isDefault:this.list[index].isDefault,
            areaCode:this.list[index].areaCode,orderId:this.orderId
          }})
      }

    },
    return1(){
      if (this.itemId){
        this.$router.push({path:'/appOrder',query:{i:this.i,itemId:this.itemId,orderId:this.orderId}})
      }else {
        this.$router.push({path:'/appOrder',query:{orderId:this.orderId}})
      }
    }
  },
};
</script>
<style scoped>
>>> .van-nav-bar .van-icon{
  color: black!important;
}
</style>
